<template>
  <div class="login-background">
    <div style="display: flex;width:30%;margin-left: 35%;height: 60%">
      <div
          style="display:flex;flex: 1; align-items:center;justify-content:center; background-color: rgba(255, 255, 255, 0.2);">
        <el-form :model="user" style="width: 65%;" :rules="rules" ref="loginRef">
          <div style="font-size: 20px;font-weight: bold;color:#ffff;text-align: center;margin-bottom: 20px;">
            登&nbsp&nbsp&nbsp&nbsp录
          </div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex; align-items: center; justify-content: space-between;">
              <el-input prefix-icon="el-icon-circle-check" placeholder="请输入验证码" style="margin-right: 8px; width: 150px;"
                        v-model="user.code"></el-input>
              <div style="display: flex; justify-content: flex-end;">
      <span
          style="display: inline-block; height: 38px; line-height: 38px; background-color: white; padding: 0 8px; border: 1px solid #ccc; border-radius: 4px;">
        <valid-code @update:value="getCode"></valid-code>
      </span>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;" @click="login">登&nbsp;录</el-button>
          </el-form-item>
          <div style="display: flex;padding-top: 15px">
            <div>还没有账号？请<span style="color: blue;cursor: pointer" @click="$router.push('/register')">&nbsp;注册</span>
            </div>
            <div style="margin-left: auto;"><span style="color:blue;;cursor: pointer"
                                                  @click="handleForgetPass"> 忘记密码</span></div>
          </div>
        </el-form>
      </div>
    </div>
    <el-dialog title="忘记密码" :visible.sync="forgetPassDialogVis" width="30%">
      <el-form :model="forgetUserForm" label-width="80px" style="padding-right: 20px">
        <el-form-item label="用户名">
          <el-input v-model="forgetUserForm.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="forgetUserForm.phone" autocomplete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forgetPassDialogVis = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ValidCode from "@/components/Validcode";

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    //验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'))
      } else {
        callback() //表示通过
      }
    };

    return {
      forgetUserForm: {},//忘记密码表单的数据
      forgetPassDialogVis: false, //默认不可见
      code: '',//验证码组件传递过来的code
      user: {
        username: '',
        password: '',
        code: '',//表单里用户输入的code

      },
      rules: {
        username: [
          {required: true, message: '账号有误', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码有误', trigger: 'blur'},
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}
        ]
      }
    }
  },
  created() {
  },
  methods: {
    handleForgetPass() { //初始化表单
      this.forgetUserForm = {}
      this.forgetPassDialogVis = true
    },
    resetPassword() {
      this.$request.put('/password', this.forgetUserForm).then(res => {
        if (res.code === '200') {
          this.$message.success('重置成功，密码已重置为默认密码（12345678）')
          this.forgetPassDialogVis = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    getCode(code) {
      this.code = code.toLowerCase()
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/login', this.user).then(res => {
            if (res.code === '200') {
              localStorage.setItem("base-user", JSON.stringify(res.data))   //存储用户数据
              this.$router.push('/')
              this.$message.success('登陆成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>
.login-background {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('../assets/login1.jpg');
  /*background-size: cover;*/
  background-size: 100% 100%
}
</style>